﻿using System;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.Common;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by: Claudia De Leon
////Date created: 04/16/2013
////ULTI-94692 Location Business Rules - Edit a Canada Location in .NET https://ultidev/browse/ULTI-94692
////*************************************************************************************************************************************

namespace Echo.Automation.Compliance.BusinessRules.Locations
{
    [TestFixture]
    [Author("Claudia De Leon", "claudia_deleon@ultimatesoftware.com")]
    [Category("Business Rules")]
    [Header("Config")]
    [Config(".NET")]
    public class EditCanadaLocation : MbUnitAutomatedTest
    {
        #region Setups / Teardowns
        UltiPro UltiPro;
        UltiProDatabaseContext Database;
        private readonly DateTime Date = DateTime.Today;

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            #region Setup database ULTIPRO_CALENDAR
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
            Database.Company.AddRollBack("iLocation");
            Database.Company.AddRollBack("Loccomp");
            Database.Company.AddRollBack("Addresses");
            Database.Company.AddRollBack("phonenumbers");
            #endregion

            #region Login as System Admin MerrillG for Web Payroll Company
            UltiPro.Pages.Login.LoginAs("MerrillG", "password", Config.LanguageCode);
            UltiPro.Pages.CompanySelector.ChooseCompanies("CALEN", "C0014");
            #endregion
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            Database.Company.RollBackDatabase();
            UltiPro.Browser.Quit();
        }
        #endregion

        [Test]
        public void TaxCodeNotSetupForCompany()
        {
            #region GIVEN I Edit a location with Code ABNTGI
            UltiPro.Macros.Navigate.ToLocationBusinessRule("ABNTGI");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            #endregion

            #region WHEN I set BC Prov Tax as the Province/Territory
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.ProvinceTerritoryDropdown, "BC Prov Tax", SelectOption.Text);
            #endregion

            #region AND Save the page
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            #endregion

            #region THEN I get an error message:"Tax code "BCPIT" not setup for "CAN No Tax Calc Group ID"."
            UltiPro.Pages.AddChgLocations.ErrorMessages.Has("Tax code \"BCPIT\" not setup for \"CAN No Tax Calc Group ID\".");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        // Defect story: https://ultidev/browse/ULTI-100500
        [Test]
        public void TaxCodeDropdownDisabledForLocationWithEmployeesAttached()
        {
            #region GIVEN I Edit a location with Code HCNB2
            UltiPro.Macros.Navigate.ToLocationBusinessRule("HCNB2");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            #endregion

            #region AND the Province/Territory dropdown is disabled
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.ProvinceTerritoryDropdown);
            #endregion

            #region WHEN I click the Reset button
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Reset);
            #endregion

            #region THEN the Province/Territory dropdown is still disable
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.ProvinceTerritoryDropdown);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        [Test]
        public void TaxInformationDisableForQuebecLocationWithEmployeesAttached()
        {
            #region GIVEN I Edit a location with Code CQE
            UltiPro.Macros.Navigate.ToLocationBusinessRule("CQE");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            #endregion

            #region WHEN I click the Reset button
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Reset);
            #endregion

            #region THEN the Province/Territory section (Province/Terr, Other tax code, QEID) is disable
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.ProvinceTerritoryDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.OtherTaxCodeDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.QuebecEmployerIdDropdown);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        [Test]
        [Row("BAKERL")]
        [Row("NTWEB")]
        public void TaxInformationDisableForNUandNTLocationWithEmployeesAttached(string locationCode)
        {
            #region GIVEN I Edit a location with Code NU/NT
            UltiPro.Macros.Navigate.ToLocationBusinessRule("CQE");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            #endregion

            #region WHEN I click the Reset button
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Reset);
            #endregion

            #region THEN the Province/Territory section (Province/Terr, Other tax code) is disable
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.ProvinceTerritoryDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.OtherTaxCodeDropdown);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        [Test]
        public void CannotDeleteLocationWithHistoryOrEmployeesAttached()
        {
            #region GIVEN that a location is tied to an employee
            int count;
            int.TryParse(Database.Company.GetValue("SELECT COUNT(location.locCode) FROM location JOIN empmloc ON location.loccode = empmloc.emlcode WHERE location.locaddresscountry = 'CAN' AND location.loccode = 'CQE'"), out count);
            Assert.GreaterThanOrEqualTo(count, 1);
            #endregion

            #region WHEN I go to the location page
            UltiPro.Macros.Navigate.ToLocationBusinessRule("CQE");
            #endregion

            #region THEN then DELETE button is disabled
            UltiPro.Pages.AddChgLocations.ButtonBar.NotEnabled(button => button.Delete);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        //Story: https://ultidev/browse/ULTI-99334
        [Test]
        public void TaxCodeDropdownEnableForLocationWithNoEmployeesAttached()
        {
            #region GIVEN I select a location that is not tied to an employee (Code CNDAU)
            int count;
            int.TryParse(Database.Company.GetValue("SELECT COUNT(location.locCode) FROM location JOIN empmloc ON location.loccode = empmloc.emlcode WHERE location.locaddresscountry = 'CAN' AND location.loccode = 'CNDAU'"), out count);
            Assert.AreEqual(count, 0);
            UltiPro.Macros.Navigate.ToLocationBusinessRule("CNDAU");
            #endregion

            #region WHEN I click the Edit button
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            #endregion
            
            #region THEN the Province/Territory section is enable with NS Prov Tax selected
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.ProvinceTerritoryDropdown);
            UltiPro.Pages.AddChgLocations.Is(dropdown => dropdown.ProvinceTerritoryDropdown, "innerHTML", "NS Prov Tax");
            #endregion
            
            #region WHEN I set the Province/Territory dropdown to QC Prov Tax
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.ProvinceTerritoryDropdown, "QC Prov Tax", SelectOption.Text);
            #endregion
            
            #region THEN completing the required fields allows to save the changes to the page
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.QuebecEmployerIdDropdown, "Executive", SelectOption.Text);
            //TODO: Update location on setup server and remove the line below in R2
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.PhyicalAddressStateProvinceDropdown, "Alberta", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Edit);
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Delete);
            UltiPro.Pages.AddChgLocations.ButtonBar.NotExists(button => button.Save);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
            
            #region Database check
            //Database.Company.Verify("SELECT loccode, LocSITWorkInStateCode, LocLITOtherCode from Location WHERE loccode = 'CNDAU'and LocSITWorkInStateCode = 'QCPIT' and LocLITOtherCode is null and LocQEIDCode = '0'", "LocationBusinessRules/TaxCodeDropdownEnableForLocWithNoEmpsAttached.xml");
            #endregion
        }

        [Test]
        public void SaveAdditionalFieldsOnLocation()
        {
            #region GIVEN I Edit a location with Code CNDAU
            UltiPro.Macros.Navigate.ToLocationBusinessRule("CNDAU");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            #endregion

            #region WHEN I edit some of the fields in the Details and Address and Contact sections
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.DescriptionTextbox.EnglishTextBox, "New description");
            UltiPro.Pages.AddChgLocations.Click(checkbox => checkbox.InactiveCheckbox);
            UltiPro.Pages.AddChgLocations.InactiveDate.Set(Date);
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.ReportingCategoryDropdown, "ALL Rep Cat", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.PhoneNumberTextbox, "7862583382");
            //TODO: Update location on setup server and remove the line below in R2
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.PhyicalAddressStateProvinceDropdown, "Alberta", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            #endregion

            #region Then the page in view mode holds the values
            UltiPro.Pages.AddChgLocations.Is(value => value.DescriptionValueEn, "innerHTML", "New description");
            UltiPro.Pages.AddChgLocations.Selected(checkbox => checkbox.InactiveCheckbox);
            UltiPro.Pages.AddChgLocations.Is(value => value.InactiveDateValue, "innerHTML", Date.ToString("MM/dd/yyyy"));
            UltiPro.Pages.AddChgLocations.Is(dropdown => dropdown.ReportingCategoryValue, "innerHTML", "LOCALL - ALL Rep Cat");
            UltiPro.Pages.AddChgLocations.Is(value => value.PhoneNumberValue, "innerHTML", "7862583382");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion

            #region Database check
            /*
             * Database.Company.Verify("SELECT loccode, LocIsInactive, LocReportingCategory, LocPhoneMainNumber FROM location WHERE loccode = 'CNDAU' AND LocIsInactive = 'Y'" +
             *                        "AND LocReportingCategory = 'LOCALL' AND LocPhoneMainNumber = '7862583382'", "LocationBusinessRules/SaveAdditionalFieldsOnLocation.xml");
             * var dbDate = Database.Company.GetValueAsDateTime("SELECT LocInactiveDate FROM location where loccode = 'CNDAU'").ToShortDateString();
             * Assert.AreEqual(Date.ToShortDateString(), dbDate);
             */
            #endregion
        }

        [Test(Order = 1)] // run last
        public void DeleteLocationCNDAU()
        {
            #region GIVEN I open a location with Code CNDAU
            UltiPro.Macros.Navigate.ToLocationBusinessRule("CNDAU");
            #endregion

            #region WHEN I Delete the location using the Delete button on the menu bar
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Delete);
            UltiPro.Alert.Accept();
            #endregion
            
            #region THEN the location is no longer available on the Summary grid
            UltiPro.Pages.LocationSummary.Is(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.Locations);
            UltiPro.Pages.LocationSummary.LocationSummaryGrid.Find(code => code.CodeFilter, FilterType.Is, "CNDAU");
            UltiPro.Pages.LocationSummary.LocationSummaryGrid.Row(column => column.CodeLink, "CNDAU").RowNotExists();
            #endregion
            
            #region Database check
            //Database.Company.Verify("SELECT COUNT (loccode) FROM location WHERE loccode = 'CNDAU'", "LocationBusinessRules/DeleteLocationCNDAU.xml");
            #endregion
        }
    }
}
